Custom Mappers and Annotations
https://gyazo.com/9fadd76eec0c3124b962acd3fd13b5e7 #Customization
Custom row mapper (sorm4j.mapping)
ResultSetTraverser
Sorm4j also could automatically map a ResultSet to an object, but you can also assign your mapping function via ResultSetTraverser.
RowMapper
Sorm4j could automatically map a row to an object, but you can also assign your mapping function via RowMapper.
code:exaplme.java
sorm.executeQuery(ParameterizedSql.of("select * from players"),
(ResultSet rs, int rowNum) ->
new Player(rs.getInt("id"), rs.getString("name"), rs.getString("address")));
Annotaion (sorm4j.mapping.annotation)
There are a few annotations that control the mapping behavior from classes to database tables (in org.nkjmlab.sorm4j.annotation).
@OrmTable can be associated with a class and specify the name of the table that the class is mapped to.
@OrmConstructor can be associated with constructor parameters and specify the names of the column.
@OrmColumn can be associated with a field and specify the name of the column.
@OrmGetter can be associated with a field and specify the getter of the column.
@OrmSetter can be associated with a field and specify the setter of the column.
@OrmIgnore can be associated with a setter/getter/field specify it should not be mapped to a column in the database (by default, Sorm4j attempts to map all fields of a given class to columns in the table associated with their class).
@OrmColumnAliasPrefix > JOIN
code:java
List<Tuple2<Guest, Player>> result = sorm.readTupleList(Guest.class, Player.class,
"select g.id as g_DOT_id, g.name as g_DOT_name, g.address as g_DOT_address, " +
"p.id as pid, p.name as p_DOT_name, p.address as p_DOT_address" +
"from guests g join players p on g.id=p.id");
@OrmColumnAliasPrefix("g") public class Guest {}
@OrmColumnAliasPrefix("p") public class Player {}